package logic

import (
	"context"
	"doc-flow/cache"
	"doc-flow/model"
	"encoding/json"
	"fmt"
	"time"

	"doc-flow/docrpc/docrpc"
	"doc-flow/docrpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type PingListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewPingListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PingListLogic {
	return &PingListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *PingListLogic) PingList(in *docrpc.PingListRequest) (*docrpc.PingListResponse, error) {
	// todo: add your logic here and delete this line
	var list []*docrpc.PingList

	result, _ := cache.Rdb.Get(cache.Ctx, fmt.Sprintf("PingList_%s", in)).Result()
	if result == "" {
		list = model.FlowList(model.DB, list, in)

		marshal, _ := json.Marshal(&list)

		cache.Rdb.Set(cache.Ctx, fmt.Sprintf("PingList_%s", in), marshal, time.Hour*2)
	} else {
		json.Unmarshal([]byte(result), &list)
	}

	return &docrpc.PingListResponse{
		List: list,
	}, nil
}
